package com.liuxvru.project.user.controller;

import com.liuxvru.common.annotation.Log;
import com.liuxvru.common.core.controller.BaseController;
import com.liuxvru.common.core.domain.AjaxResult;
import com.liuxvru.common.core.page.TableDataInfo;
import com.liuxvru.common.enums.BusinessType;
import com.liuxvru.common.utils.poi.ExcelUtil;
import com.liuxvru.project.domain.ProjectAnnouncement;
import com.liuxvru.project.service.IProjectAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目公告Controller
 *
 * @author liuxvru
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/user/project/projectAnnouncement")
public class UserProjectAnnouncementController extends BaseController {
    @Autowired
    private IProjectAnnouncementService projectAnnouncementService;

    /**
     * 查询项目公告列表
     */
    @GetMapping("/list/{projectId}")
    public TableDataInfo list(@PathVariable("projectId") Long projectId, ProjectAnnouncement projectAnnouncement) {
        startPage();
        projectAnnouncement.setProjectId(projectId);
        List<ProjectAnnouncement> list = projectAnnouncementService.listByProjectId(projectId,projectAnnouncement);
        return getDataTable(list);
    }
    /**
     * 查询项目公告列表
     */
    @GetMapping("/listMy/{projectId}")
    public TableDataInfo listMy(@PathVariable("projectId") Long projectId, ProjectAnnouncement projectAnnouncement) {
        startPage();
        projectAnnouncement.setProjectId(projectId);
        projectAnnouncement.setCreatedBy(getUserId());
        List<ProjectAnnouncement> list = projectAnnouncementService.listByProjectId(projectId,projectAnnouncement);
        return getDataTable(list);
    }
    /**
     * 查询项目公告列表
     */
    @GetMapping("/listAll/{projectId}")
    public AjaxResult listAll(@PathVariable("projectId") Long projectId, ProjectAnnouncement projectAnnouncement) {
        projectAnnouncement.setProjectId(projectId);
        List<ProjectAnnouncement> list = projectAnnouncementService.listByProjectId(projectId,projectAnnouncement);
        return success(list);
    }

    /**
     * 导出项目公告列表
     */
    @Log(title = "项目公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectAnnouncement projectAnnouncement) {
        List<ProjectAnnouncement> list = projectAnnouncementService.selectProjectAnnouncementList(projectAnnouncement);
        ExcelUtil<ProjectAnnouncement> util = new ExcelUtil<ProjectAnnouncement>(ProjectAnnouncement.class);
        util.exportExcel(response, list, "项目公告数据");
    }

    /**
     * 获取项目公告详细信息
     */
    @GetMapping(value = "/{announcementId}")
    public AjaxResult getInfo(@PathVariable("announcementId") Long announcementId) {
        return success(projectAnnouncementService.getById(announcementId));
    }

    /**
     * 新增项目公告
     */
    @Log(title = "项目公告", businessType = BusinessType.INSERT)
    @PostMapping("/{projectId}")
    public AjaxResult add(@PathVariable("projectId") Long projectId, @RequestBody ProjectAnnouncement projectAnnouncement) {
        return toAjax(projectAnnouncementService.add(projectId,projectAnnouncement));
    }

    /**
     * 修改项目公告
     */
    @Log(title = "项目公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectAnnouncement projectAnnouncement) {
        return toAjax(projectAnnouncementService.edit(projectAnnouncement));
    }

    /**
     * 删除项目公告
     */
    @Log(title = "项目公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{announcementIds}")
    public AjaxResult remove(@PathVariable Long[] announcementIds) {
        return toAjax(projectAnnouncementService.removeById(announcementIds));
    }
}
